﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="EditProductInfoForm.aspx.cs"
    Inherits="FX.CC.Andon.WebSite.BasicDataManage.EditProductInfoForm" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="../lib/boot.js" type="text/javascript"></script>
    <script src="swfupload/swfupload.js" type="text/javascript"></script>
    <style type="text/css">
        .actionIcons span
        {
            width: 16px;
            height: 16px;
            display: inline-block;
            background-position: 50% 50%;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <form id="form1" method="post" enctype="multipart/form-data">
    <input name="PrdID" class="mini-hidden" />
    <fieldset style="border: solid 1px #aaa; padding: 5px;">
        <legend>产品信息</legend>
        <div style="padding-left: 11px; padding-bottom: 5px;">
            <table style="table-layout: fixed; width: 100%;">
                <tr>
                    <td style="width: 100px;" align="right">
                        产品编号：
                    </td>
                    <td style="width: 200px;">
                        <input name="PrdCode" class="mini-textbox" style="width: 200px;" required="true"
                            onvaluechanged="onPrdCodeChanged" />
                    </td>
                    <td style="width: 100px;" align="right">
                        产品名称：
                    </td>
                    <td style="width: 200px;">
                        <input name="PrdName" class="mini-textbox" style="width: 200px;" required="true" />
                    </td>
                </tr>
                <tr>
                    <td style="width: 100px;" align="right">
                        检验指导书：
                    </td>
                    <td colspan="3">
                        <input id="CheckFile" name="CheckFile" class="mini-fileupload" name="AdbannerUrl"
                            limittype="*.xlsx;*.xls;*.pdf" style="width: 100%;" flashurl="swfupload/swfupload.swf"
                            uploadurl="../service/BasicDataManageHandler.ashx?action=CheckFileUpload" uploadonselect="true"
                            onuploadsuccess="onCheckFileUploadSuccess" />
                    </td>
                </tr>
                <tr>
                    <td style="width: 100px;" align="right">
                        操作指导书：
                    </td>
                    <td colspan="3">
                        <input id="OpFile" name="OpFile" class="mini-fileupload" name="AdbannerUrl" limittype="*.xlsx;*.xls;*.pdf"
                            style="width: 100%;" flashurl="swfupload/swfupload.swf" uploadurl="../service/BasicDataManageHandler.ashx?action=OpFileUpload"
                            uploadonselect="true" onuploadsuccess="onOpFileUploadSuccess" />
                    </td>
                </tr>
                <tr>
                    <td style="width: 100px;" align="right">
                        备注：
                    </td>
                    <td colspan="3">
                        <input name="PrdDesc" class="mini-textarea" style="width: 100%; height: 50px;" />
                    </td>
                </tr>
            </table>
        </div>
    </fieldset>
    <table>
        <tr>
            <td>
                <fieldset style="border: solid 1px #aaa; padding: 2px; height: 300px; width: 260px;">
                    <legend>工序</legend>
                    <div class="mini-toolbar" style="padding: 2px; border-bottom: 0px;">
                        <table style="width: 100%;">
                            <tr>
                                <td style="width: 100%;" align="left">
                                    <a class="mini-button" iconcls="icon-add" onclick="addGx();" plain="true">添加</a>
                                </td>
                            </tr>
                        </table>
                    </div>
                    <div class="mini-fit">
                        <div id="datagrid1" class="mini-datagrid" style="width: 100%; height: 100%;" idfield="FlowID"
                            allowcelledit="true" allowcellselect="true" showfooter="false" onrowclick="onRowclick"
                            selectonload="true">
                            <div property="columns">
                                <div field="ProcessIdx" width="50%" headeralign="center" allowsort="true">
                                    工序数量
                                </div>
                                <div field="ModelMin" width="50%" headeralign="center" allowsort="true">
                                    标准换模时间
                                    <input property="editor" class="mini-spinner" minvalue="0" vtype="int" style="width: 100%;" />
                                </div>
                            </div>
                        </div>
                    </div>
                </fieldset>
            </td>
            <td>
                <fieldset style="border: solid 1px #aaa; padding: 2px; height: 300px; width: 200px;">
                    <legend>检验指导书显示页数</legend>
                    <div class="mini-toolbar" style="padding: 2px; border-bottom: 0px;">
                        <table style="width: 100%;">
                            <tr>
                                <td style="width: 100%;" align="left">
                                    <a class="mini-button" iconcls="icon-add" onclick="addJy();" plain="true">添加</a>
                                </td>
                            </tr>
                        </table>
                    </div>
                    <div class="mini-fit">
                        <div id="datagrid2" class="mini-datagrid" style="width: 100%; height: 100%;" showfooter="false">
                            <div property="columns">
                                <div field="PageNo" width="80%" headeralign="center">
                                    检验指导书页数
                                </div>
                                <div cellcls="actionIcons" name="action" width="20%" headeralign="center" align="center"
                                    renderer="onJyActionRenderer" cellstyle="padding:0;">
                                    操作</div>
                            </div>
                        </div>
                    </div>
                </fieldset>
            </td>
            <td>
                <fieldset style="border: solid 1px #aaa; padding: 2px; height: 300px; width: 200px;">
                    <legend>作业指导书显示页数</legend>
                    <div class="mini-toolbar" style="padding: 2px; border-bottom: 0px;">
                        <table style="width: 100%;">
                            <tr>
                                <td style="width: 100%;" align="left">
                                    <a class="mini-button" iconcls="icon-add" onclick="addZy();" plain="true">添加</a>
                                </td>
                            </tr>
                        </table>
                    </div>
                    <div class="mini-fit">
                        <div id="datagrid3" class="mini-datagrid" style="width: 100%; height: 100%;" showfooter="false"
                            allowcellselect="true">
                            <div property="columns">
                                <div field="PageNo" width="80%" headeralign="center">
                                    作业指导书页数
                                </div>
                                <div cellcls="actionIcons" name="action" width="20%" headeralign="center" align="center"
                                    renderer="onZyActionRenderer" cellstyle="padding:0;">
                                    操作</div>
                            </div>
                        </div>
                    </div>
                </fieldset>
            </td>
        </tr>
    </table>
    <div style="text-align: center; padding: 10px;">
        <a class="mini-button" iconcls="icon-save" onclick="onOk" style="width: 60px; margin-right: 20px;">
            保存</a> <a class="mini-button" iconcls="icon-cancel" onclick="onCancel" style="width: 60px;">
                取消</a>
    </div>
    </form>
    <script type="text/javascript">
        mini.parse();
        var form = new mini.Form("form1");
        var action;
        var grid1 = mini.get("datagrid1");
        var grid2 = mini.get("datagrid2");
        var grid3 = mini.get("datagrid3");
        function onPrdCodeChanged(e) {
            if (e.value) {
                $.ajax({
                    url: "../service/PublicHandler.ashx?action=CheckedCode&tableName=UT_Product&fieldName=PrdCode&Code=" + e.value,
                    success: function (text) {
                        if (text > 0) {
                            mini.alert(e.value + "已经存在！请重新输入！");
                            mini.getbyName("PrdCode").setValue("");
                        }
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        alert(jqXHR.responseText);
                    }
                });
            }
        }
        //添加工序
        function addGx() {
            var obj = grid1.getData();
            var data = [];
            var o = {};
            o.FlowID = "00000000-0000-0000-0000-000000000000";
            o.ProcessIdx = obj.length + 1;
            o.ModelMin = 10;
            data.push(o);
            grid1.addRows(data);
        }
        var JyItems = new Array(); //检验
        //添加检验指导书页数
        function addJy() {
            var rows = grid1.getSelected();
            if (rows) {
                mini.open({
                    url: "BasicDataManage/PageNoInfoForm.aspx",
                    title: "添加检验指导书页数", width: 280, height: 115,
                    ondestroy: function (action) {
                        if (action == "ok") {
                            var iframe = this.getIFrameEl();
                            var data = iframe.contentWindow.GetData();
                            data = mini.clone(data);    //必须
                            data.ProcessIdx = rows.ProcessIdx;
                            if (data) {
                                SaveJyItems(rows, data);
                            }
                        }
                    }
                });
            }
            else {
                mini.alert("请选择设备！");
            }
        }
        function SaveJyItems(rows, data) {
            if (action == "edit") {
                if (rows.JyItems != null) {
                    rows.JyItems.push(data);
                    addJyItem(rows);
                }
                else {
                    JyItems.push(data);
                    rows.JyItems = JyItems;
                    if (rows.JyItems != "" && rows.JyItems != null) {
                        addJyItem(rows);
                    }
                }
            }
            else {
                JyItems.push(data);
                rows.JyItems = JyItems;
                if (rows.JyItems != "" && rows.JyItems != null) {
                    addJyItem(rows);
                }
            }
        }

        function addJyItem(obj) {
            grid2.clearRows();
            var jy = [];
            for (var i = 0; i < obj.JyItems.length; i++) {
                if (obj.ProcessIdx == obj.JyItems[i].ProcessIdx) {
                    jy.push(obj.JyItems[i]);
                }
            }
            grid2.addRows(jy);
        }
        var ZyItems = new Array(); //作业
        //添加作业指导书页数
        function addZy() {
            var rows = grid1.getSelected();
            if (rows) {
                mini.open({
                    url: "BasicDataManage/PageNoInfoForm.aspx",
                    title: "添加作业指导书页数", width: 280, height: 115,
                    ondestroy: function (action) {
                        if (action == "ok") {
                            var iframe = this.getIFrameEl();
                            var data = iframe.contentWindow.GetData();
                            data = mini.clone(data);    //必须
                            data.ProcessIdx = rows.ProcessIdx;
                            if (data) {
                                SaveZyItems(rows, data);
                            }
                        }
                    }
                });
            }
            else {
                mini.alert("请先选择工序！");
            }
        }
        function SaveZyItems(rows, data) {
            if (action == "edit") {
                if (rows.ZyItems != null) {
                    rows.ZyItems.push(data);
                    addZyItem(rows);
                }
                else {
                    ZyItems.push(data);
                    rows.ZyItems = ZyItems;
                    if (rows.ZyItems != "" && rows.ZyItems != null) {
                        addZyItem(rows);
                    }
                }
            }
            else {
                ZyItems.push(data);
                rows.ZyItems = ZyItems;
                if (rows.ZyItems != "" && rows.ZyItems != null) {
                    addZyItem(rows);
                }
            }
        }

        function addZyItem(obj) {
            grid3.clearRows()
            var zy = [];
            for (var i = 0; i < obj.ZyItems.length; i++) {
                if (obj.ProcessIdx == obj.ZyItems[i].ProcessIdx) {
                    zy.push(obj.ZyItems[i]);
                }
            }
            grid3.addRows(zy);
        }

        //删除设备行
        function delEqRow(row_uid) {
            var row = grid1.getRowByUID(row_uid);
            if (row) {
                grid1.removeRow(row); //删除设备
            }
        }
        function onEqActionRenderer(e) {
            var grid = e.sender;
            var record = e.record;
            var uid = record._uid;
            var rowIndex = e.rowIndex;
            var s = '<span class="icon-remove" title="删除" onclick="delEqRow(' + uid + ')"></span>';
            return s;
        }

        //删除检验指导书页数
        function delJyRow(row_uid) {
            var rows = grid1.getSelected();
            var row = grid2.getRowByUID(row_uid);
            if (rows) {
                if (row) {
                    for (var i = 0; i < rows.JyItems.length; i++) {
                        if (rows.JyItems[i].PageNo == row.PageNo && rows.JyItems[i].ProcessIdx == row.ProcessIdx) {
                            rows.JyItems.remove(rows.JyItems[i]);
                        }
                    }
                    grid2.removeRow(row);
                }
            }
            else {
                mini.alert("请先选择工序！");
            }

        }
        function onJyActionRenderer(e) {
            var grid = e.sender;
            var record = e.record;
            var uid = record._uid;
            var rowIndex = e.rowIndex;
            var s = '<span class="icon-remove" title="删除" onclick="delJyRow(' + uid + ')"></span>';
            return s;
        }
        //删除作业指导书页数
        function delZyRow(row_uid) {
            var rows = grid1.getSelected();
            var row = grid3.getRowByUID(row_uid);
            if (rows) {
                if (row) {
                    for (var i = 0; i < rows.ZyItems.length; i++) {
                        if (rows.ZyItems[i].PageNo == row.PageNo && rows.ZyItems[i].ProcessIdx == row.ProcessIdx) {
                            rows.ZyItems.remove(rows.ZyItems[i]);
                        }
                    }
                    grid3.removeRow(row);
                }
            }
            else {
                mini.alert("请先选择设备！");
            }
        }
        function onZyActionRenderer(e) {
            var grid = e.sender;
            var record = e.record;
            var uid = record._uid;
            var rowIndex = e.rowIndex;
            var s = '<span class="icon-remove" title="删除" onclick="delZyRow(' + uid + ')"></span>';
            return s;
        }


        function onCheckFileUploadSuccess(e) {
            mini.get("CheckFile").setValue(e.serverData);
        }
        function onOpFileUploadSuccess(e) {
            mini.get("OpFile").setValue(e.serverData);
        }

        //行点击事件添加到检查列表中
        function onRowclick(e) {
            var row = e.record;
            if (row) {
                addItem(row);
            }
        }

        function addItem(obj) {
            grid2.clearRows()
            var jy = [];
            var zy = [];
            if (obj.JyItems != null) {
                for (var i = 0; i < obj.JyItems.length; i++) {
                    if (obj.ProcessIdx == obj.JyItems[i].ProcessIdx) {
                        jy.push(obj.JyItems[i]);
                    }
                }
            }
            grid2.addRows(jy);
            grid3.clearRows()
            if (obj.ZyItems != null) {
                for (var i = 0; i < obj.ZyItems.length; i++) {
                    if (obj.ProcessIdx == obj.ZyItems[i].ProcessIdx) {
                        zy.push(obj.ZyItems[i]);
                    }
                }
            }
            grid3.addRows(zy);
        }

        function SaveData() {
            var o = form.getData();
            form.validate();
            if (form.isValid() == false) return;
            var flowData = grid1.getData();
            var jyData = [];
            var zyData = [];
            if (flowData) {
                for (var i = 0; i < flowData.length; i++) {
                    if (flowData[i].JyItems != null) {
                        for (var j = 0; j < flowData[i].JyItems.length; j++) {
                            if (flowData[i].ProcessIdx == flowData[i].JyItems[j].ProcessIdx) {
                                jyData.push(flowData[i].JyItems[j]);
                            }
                        }
                    }
                    if (flowData[i].ZyItems != null) {
                        for (var k = 0; k < flowData[i].ZyItems.length; k++) {
                            if (flowData[i].ProcessIdx == flowData[i].ZyItems[k].ProcessIdx) {
                                zyData.push(flowData[i].ZyItems[k]);
                            }
                        }
                    }
                }
            }
            SaveDatas(o, flowData, jyData, zyData);
        }

        function SaveDatas(o, flowData, jyData, zyData) {
            o.arrFlowList = flowData;
            o.arrJyList = jyData;
            o.arrZyList = zyData;
            var json = mini.encode([o]);
            if (action == "edit") {
                $.ajax({
                    url: "../Service/BasicDataManageHandler.ashx?action=ModifyProduct",
                    data: { data: json },
                    type: 'POST',
                    success: function (text) {
                        CloseWindow("save");
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        alert(jqXHR.responseText);
                    }
                });
            }
            else {
                $.ajax({
                    url: "../Service/BasicDataManageHandler.ashx?action=AddProduct",
                    data: { data: json },
                    type: 'POST',
                    success: function (text) {
                        CloseWindow("save");
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        alert(jqXHR.responseText);
                    }
                });
            }
        }

        ////////////////////
        //标准方法接口定义
        function SetData(data) {
            data = mini.clone(data);
            action = data.action;
            if (data.action == "edit") {
                //跨页面传递的数据对象，克隆后才可以安全使用
                $.ajax({
                    url: "../service/BasicDataManageHandler.ashx?action=GetProductInfo&PrdID=" + data.PrdID,
                    cache: false,
                    success: function (text) {
                        var o = mini.decode(text);
                        $.ajax({
                            url: "../service/BasicDataManageHandler.ashx?action=GetFlowInfo&PrdID=" + o.PrdID,
                            cache: false,
                            success: function (text) {
                                var result = mini.decode(text);
                                grid1.clearRows();
                                grid1.addRows(result);
                                if (result != "") {
                                    grid2.clearRows();
                                    grid2.addRows(result[0].JyItems);
                                    grid3.clearRows();
                                    grid3.addRows(result[0].ZyItems);
                                }
                            }
                        });
                        form.setData(o);
                        form.setChanged(false);
                        mini.get("CheckFile").setText(o.CheckFile);
                        mini.get("OpFile").setText(o.OpFile);
                    }
                });
            }
        }
        function GetData() {
            var o = form.getData();
            return o;
        }
        function CloseWindow(action) {
            if (action == "close" && form.isChanged()) {
                if (confirm("数据被修改了，是否先保存？")) {
                    return false;
                }
            }
            if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
            else window.close();
        }

        function onOk(e) {
            SaveData();
        }
        function onCancel(e) {
            CloseWindow("cancel");
        }
    </script>
</body>
</html>
